<template>
  <el-dialog :title="dialogTitle" :visible="showDialog" @close="closeDialog">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          size="mini"
          placeholder="2-10个字符"
          autocomplete="off"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          size="mini"
          placeholder="2-10个字符"
          autocomplete="off"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select
          v-model="form.managerId"
          size="mini"
          placeholder="请选择负责人"
          style="width: 80%"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          size="mini"
          type="textarea"
          style="width: 80%"
          placeholder="1-100个字符"
          :rows="4"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="submit"
            >确定</el-button>
            <el-button size="mini" @click="closeDialog">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getManagerList, addDepartment, getDepDetail, editDep } from '@/api/department'
export default {
  props: {
    showDialog: Boolean,
    originDeps: {
      type: Array,
      required: true
    },
    currentId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门名称的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              let deps = this.originDeps
              if (this.form.id) {
                deps = deps.filter((item) => item.id !== this.form.id)
              }
              deps.some((item) => item.name === value)
                ? callback(new Error('部门中已经有该名称了'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门编码的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              this.originDeps.some((item) => item.code === value && item.id !== this.form.id)
                ? callback(new Error('部门中已经有该编码了'))
                : callback()
            },
            trigger: 'blur'
          }
        ],
        managerId: [{ required: true, message: '部门负责人不能为空' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 100,
            message: '部门介绍的长度为1-100个字符',
            trigger: 'blur'
          }
        ]
      },
      managerList: []
    }
  },
  computed: {
    dialogTitle() {
      return `${this.form.id ? '编辑' : '新增'}部门`
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    closeDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      }
    },
    async getManagerList() {
      this.managerList = await getManagerList()
    },
    async submit() {
      await this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          const { currentId: pid } = this
          this.form.id ? await editDep(this.form.id, { ...this.form }) : await addDepartment({ pid, ...this.form })
          this.$message.success(`${this.dialogTitle}成功`)
          this.closeDialog()
          this.$emit('updateDeps')
        }
      })
    },
    async getDepDetail(id) {
      this.form = await getDepDetail(id)
    }
  }
}
</script>

<style>
</style>
